package com.keda.controller;

import com.keda.model.*;
import com.keda.service.IndexService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @author Tonygogo
 */
@WebServlet("/index") // 采用注解的方式进行Servlet的映射
public class IndexServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 1. 接收参数

        // 2. 调用Service业务逻辑处理
        IndexService indexService = new IndexService();

        // 查询特价商品
        List<Product> specialProducts = indexService.findSpecialProducts();
        request.setAttribute("specialProducts", specialProducts);

        // 查询热门商品
        List<Product> hotProducts = indexService.findHotProducts();
        request.setAttribute("hotProducts", hotProducts);

        // 3. 返回结果
        // Servlet跳转到前端有几种方式：请求转发和重定向
        request.getRequestDispatcher("index.jsp").forward(request, response); // 转发
    }
}
